package cn.iocoder.yudao.module.pickbook.service.voiceactors;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.voiceactors.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.voiceactors.VoiceActorsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.voiceactors.VoiceActorsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 配音 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VoiceActorsServiceImpl implements VoiceActorsService {

    @Resource
    private VoiceActorsMapper voiceActorsMapper;

    @Override
    public Long createVoiceActors(VoiceActorsSaveReqVO createReqVO) {
        // 插入
        VoiceActorsDO voiceActors = BeanUtils.toBean(createReqVO, VoiceActorsDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        voiceActors.setId(id);

        voiceActorsMapper.insert(voiceActors);

        // 返回
        return voiceActors.getId();
    }

    @Override
    public void updateVoiceActors(VoiceActorsSaveReqVO updateReqVO) {
        // 校验存在
        validateVoiceActorsExists(updateReqVO.getId());
        // 更新
        VoiceActorsDO updateObj = BeanUtils.toBean(updateReqVO, VoiceActorsDO.class);
        voiceActorsMapper.updateById(updateObj);
    }

    @Override
    public void deleteVoiceActors(Long id) {
        // 校验存在
        validateVoiceActorsExists(id);
        // 删除
        voiceActorsMapper.deleteById(id);
    }

    @Override
        public void deleteVoiceActorsListByIds(List<Long> ids) {
        // 删除
        voiceActorsMapper.deleteByIds(ids);
        }


    private void validateVoiceActorsExists(Long id) {
        if (voiceActorsMapper.selectById(id) == null) {
            throw exception(VOICE_ACTORS_NOT_EXISTS);
        }
    }

    @Override
    public VoiceActorsDO getVoiceActors(Long id) {
        return voiceActorsMapper.selectById(id);
    }

    @Override
    public PageResult<VoiceActorsDO> getVoiceActorsPage(VoiceActorsPageReqVO pageReqVO) {
        return voiceActorsMapper.selectPage(pageReqVO);
    }

}